package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseLGAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.OrderAllBean;
import com.wnhz.work.yz.bean.OrderDetailsBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.NoScrollListView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---订单详情
 */
public class OrderDetailsActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private NoScrollListView myListView;
    private BaseLGAdapter<OrderDetailsBean> adapter;

    private TextView tv_look;
    private String orderId;

    private TextView tv_order_code;
    private TextView tv_type;
    private TextView tv_order_time;
    private TextView tv_pay_way;
    private TextView tv_order_form;
    private TextView tv_order_head;
    private TextView tv_order_message;
    private TextView tv_all_price;
    private TextView tv_carriage;
    private TextView tv_price;
    private TextView tv_remark;

    private List<OrderDetailsBean> goodsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);

        initData();
        initView();
    }

    private void initData() {
        orderId = getIntent().getStringExtra("orderId");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        tv_remark= (TextView) findViewById(R.id.tv_remark);
        tv_order_code = (TextView) findViewById(R.id.tv_order_code);//订单号
        tv_type = (TextView) findViewById(R.id.tv_type);//订单状态
        tv_order_time = (TextView) findViewById(R.id.tv_order_time);//下单时间
        tv_pay_way = (TextView) findViewById(R.id.tv_pay_way);//支付方式
        tv_order_form = (TextView) findViewById(R.id.tv_order_form);//发票类型
        tv_order_head = (TextView) findViewById(R.id.tv_order_head);//发票抬头
        tv_order_message = (TextView) findViewById(R.id.tv_order_message);//发票信息
        tv_all_price = (TextView) findViewById(R.id.tv_all_price);//商品总价
        tv_carriage = (TextView) findViewById(R.id.tv_carriage);//运费
        tv_price = (TextView) findViewById(R.id.tv_price);//共需支付

        tv_look = (TextView) findViewById(R.id.tv_look);
        tv_look.setOnClickListener(this);
        findViewById(R.id.tv_apply).setOnClickListener(this);
        findViewById(R.id.tv_true).setOnClickListener(this);
        myListView = (NoScrollListView) findViewById(R.id.myListView);
        upload();
        adapter = new BaseLGAdapter<OrderDetailsBean>(this, goodsList) {
            @Override
            protected int getLayoutId() {
                return R.layout.item_start_order_shopping;
            }

            @Override
            protected void onBind(BaseViewHolder holder, int position) {
                ImageView iv_img = holder.getView(R.id.shopping_img);
                x.image().bind(iv_img, goodsList.get(position).getGoods_spic());
                holder.setTextView(R.id.title_tv, goodsList.get(position).getGoods_name());
                holder.setTextView(R.id.material, goodsList.get(position).getSku_info());
                holder.setTextView(R.id.tv_price, "¥ " + goodsList.get(position).getGoods_price());
                holder.setTextView(R.id.number_tv, "x" + goodsList.get(position).getGoods_nums());
            }
        };
        myListView.setAdapter(adapter);

    }

    private static final String TAG = "OrderDetailsActivity";

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_GETORDERDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:orderId=== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    OrderAllBean bean = new OrderAllBean();
                    Gson gson = new Gson();
                    bean = gson.fromJson(jsonObject.optString("info"), OrderAllBean.class);

                    tv_order_code.setText(bean.getOrder_info().getOrder_no());
                    tv_type.setText(bean.getOrder_info().getOrder_desc());
                    tv_order_time.setText(bean.getOrder_info().getOrder_time());
                    tv_pay_way.setText(bean.getOrder_info().getPay_way());
                    tv_order_form.setText(bean.getReceipt().getInvoice_type());
                    tv_order_head.setText(bean.getReceipt().getInvoice_title());
                    tv_order_message.setText(bean.getReceipt().getContent());
                    tv_all_price.setText("¥ " + bean.getOrder_info().getTotal_price());
                    tv_carriage.setText("+¥ " + bean.getOrder_info().getExpress_fee());
                    tv_price.setText("¥ " + bean.getOrder_info().getTotal_fee());
                    tv_remark.setText("" + bean.getOrder_info().getRemark());
                    goodsList = bean.getOrder_goods();
                    Log.e(TAG, "onSuccess: " + goodsList.size() + "====" + bean.getOrder_info().getRemark()+"]");


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter.updateAdapter(goodsList);
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //申请售后
            case R.id.tv_apply:
                break;

            //查看物流
            case R.id.tv_look:
                shopProgress(orderId);

                break;

            //确认收货
            case R.id.tv_true:
                break;

            default:
                break;
        }
    }

    private void shopProgress(final String orderId) {
        if (orderId == null || orderId.equals("")) {
            MyToast("请刷新页面");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderId", orderId);
        XUtil.Post(Url.ORDER_EXPRESSINFO, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("------", "R== " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        //  MyToast(info);
                        //   upload(type);
//                        Gson gson = new Gson();
//                        ShopProgressBean bean = gson.fromJson(result, ShopProgressBean.class);
//                        ShopProgressBean.InfoEntity infoEnt = bean.getInfo();
                        Intent intent = new Intent(OrderDetailsActivity.this, ShopProgressActivity.class);
                        intent.putExtra("orderId", orderId);
                        startActivity(intent);
                    } else {
                        if ("无效的订单".equals(info)) {

                            MyToast("此订单还没物流");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
//                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
}
